#############################################
library(haven)
library(tidyverse)
library(gridExtra)
library(scales)

dat <- read_dta("~/Desktop/POQ_replication.dta")

dat$OwnPID[dat$OwnPID == ""] <- "Independent"

dat <- dat %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc")

dat <- dat %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_gender = ifelse(d_gender==1,"Male","Female"),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_CollegePlus = ifelse(d_education > 4,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,"Most Important","Not Most Important"),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         vreligious = ifelse(d_religveryimp ==1 & d_religattendweekly==1,1,NA),
         vreligious = ifelse(d_religveryimp ==0 & d_religattendweekly==0,0,vreligious))

dat$sample3 <- factor(dat$sample3, 
                      levels=c('Donor (2018)', 'Affluent', 'General Population'))

abortdat <- dat %>%
  dplyr::select(surveyid, OwnPID, sample3, d_gender, d_CollegePlus, vreligious, ts_state, imp_abortion, imp_abortionmost, starts_with("i_abort"), sa_attend_meetings, sa_attend_protest, sa_contact_official, sa_donate_money, sa_work_candidate, sa_political_sign, d_yearborn)


# Create NES Scale by combining split question and turning into a factor
abortdat <- abortdat  %>%
  mutate(i_abortion1 = ifelse(i_abortion2 == 1,1,i_abortion1),
         i_abortion1 = ifelse(i_abortion2 == 2,2,i_abortion1),
         i_abortion1 = ifelse(i_abortion2 == 3,3,i_abortion1),
         i_abortion1 = ifelse(i_abortion2 == 4,4,i_abortion1),
         i_abortion1 = ifelse(i_abortion2 == 5,NA,i_abortion1)) %>%
  rowwise() %>%
         mutate(activism_scale = sum(c_across(starts_with("sa_")), na.rm = T)) %>%
  rename(abortscale = i_abortion1) %>%
  dplyr::select(-c(i_abortion2))

abortdat <- abortdat  %>%
  mutate(abortscale_f = ifelse(abortscale==1,"Never",NA),
         abortscale_f = ifelse(abortscale==2,"Rape, incest, or when the woman's life is in danger",abortscale_f),
         abortscale_f = ifelse(abortscale==3,"Only if needed",abortscale_f),
         abortscale_f = ifelse(abortscale==4,"Always",abortscale_f))

abortdat$abortscale_f <- factor(abortdat$abortscale_f, 
                              levels = c("Never", "Rape, incest, or when the woman's life is in danger", "Only if needed","Always"))

#################################################################################################
# Figure 1: By Party GenPop
# Unweighted
#################################################################################################

abortdat %>%
  filter(sample3 == "General Population") %>%
  count(OwnPID)

rawdat <- abortdat %>%
  filter(sample3 == "General Population") %>%
  group_by(OwnPID) %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))
rawdat$OwnPID <- factor(rawdat$OwnPID, levels = c("Democrat", "Independent", "Republican"))

rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=OwnPID)) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#3263a8","dark grey","#a83238")) +
  stat_summary(fun.data = mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "Among surveyed Republicans, Independents, and Democrats",
       fill="") + 
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) + 
  theme_bw() + 
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))

sumdat <- rawdat %>% select(Variable_f, value, OwnPID) %>% 
  group_by(OwnPID, Variable_f) %>%
  summarize(mean = mean(value, na.rm = T))
sumdat

#################################################################################################
# Figure 1: By Party GenPop
# Weighted, for supplemental materials
#################################################################################################
wgt <- read_csv("~/Desktop/GenPopWgts.csv")

wgt <- wgt %>% 
  rename(RakingWgtGP = RakingWgt) %>%
  dplyr::select(surveyid,RakingWgtGP)

abortdat <- left_join(abortdat, wgt)

rawdat <- abortdat %>%
  filter(sample3 == "General Population") %>%
  group_by(OwnPID) %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 


rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

#original ordering of the data (by average overall support)
mean <- tapply(rawdat$value, rawdat$Variable, mean, na.rm=TRUE)
poo2 <- sort(mean)

w.mean <- rawdat %>%
  group_by(Variable,OwnPID) %>%
  summarize(Mean = weighted.mean(value,RakingWgtGP,na.rm=TRUE))

mean <- rawdat %>%
  group_by(Variable,OwnPID) %>%
  summarize(Mean = mean(value,na.rm=TRUE))

poo <- sort(w.mean$Mean)
w.mean$Variable_f <- factor(w.mean$Variable, levels = names(poo2))
w.mean$OwnPID <- factor(w.mean$OwnPID, levels = c("Democrat", "Independent", "Republican"))

#Figure C1
Fig1.w <- w.mean %>% 
  ggplot(aes(x=Mean,y=Variable_f,fill=OwnPID)) +
  geom_col(position="dodge") +
  scale_y_discrete(labels=wrap_format(30)) +
  scale_x_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +
  theme(legend.position="bottom") +
  guides(fill = guide_legend(reverse = TRUE)) +
  scale_fill_manual(values = c("#3263a8","dark grey","#a83238")) +
  labs(x = "", fill='',
       y = "", title = "Support for abortion if...",
       subtitle = "Among surveyed Republicans, Independents, and Democrats (weighted)") + 
  geom_vline(xintercept=.5,lty=2)
Fig1.w


#################################################################################
# SI: Use different abortion scale among general population
##################################################################################

scale <- abortdat %>%
  group_by(OwnPID) %>%
  count(abortscale_f) %>%
  filter(!is.na(abortscale_f)) %>%
  mutate(Pct = n/sum(n))

#Figure F2
scale %>%
  ggplot(aes(x=abortscale_f, y = Pct, fill=OwnPID)) +
  geom_col(position="dodge") +
  coord_flip() +
  scale_fill_manual(values = c("#3263a8","dark grey","#a83238")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") +
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "Among surveyed Republicans, Independents, and Democrats",
       fill="") +
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))


#################################################################################################
## SI: Use different abortion scale among republicans, broken down by survey sample
#################################################################################################

scale <- abortdat %>%
  group_by(sample3) %>%
  filter(OwnPID == "Republican") %>%
  count(abortscale_f) %>%
  filter(!is.na(abortscale_f)) %>%
  mutate(Pct = n/sum(n))

#Figure F3
scale %>%
  ggplot(aes(x=abortscale_f, y = Pct, fill=sample3)) +
  geom_col(position="dodge") +
  coord_flip() + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "Among Republicans: Donors, Affluent, and General Population",
       fill="") + 
  scale_x_discrete(labels=wrap_format(30)) +
  scale_fill_manual(values = c("#009E73", "#0072B2","#D55E00"), labels = c("Donor", "Affluent", "General Population")) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) + 
  theme_bw() + 
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))


#################################################################################################
# SI: ANES Scale By Issue Importance, Among Republican Donors
#################################################################################################

scale <- abortdat %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "Donor (2018)") %>%
  group_by(imp_abortionmost) %>%
  mutate(imp_abortionmost = ifelse(imp_abortionmost=="Most Important","Most",imp_abortionmost),
         imp_abortionmost = ifelse(imp_abortionmost=="Not Most Important","Not",imp_abortionmost)) %>%
  count(abortscale_f) %>%
  filter(!is.na(abortscale_f)) %>%
  mutate(Pct = n/sum(n))

#Figure F4
Fig3ASI <- scale %>%
  ggplot(aes(x=abortscale_f, y = Pct, fill=imp_abortionmost)) +
  geom_col(position="dodge") +
  coord_flip() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By issue importance",
       fill="Importance") + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))
Fig3ASI



#################################################################################################
# SI: ANES Scale by State Policy on rape exception, among republican donors
#################################################################################################
# https://www.guttmacher.org/state-policy/explore/overview-abortion-laws?gclid=EAIaIQobChMIm8uy-cSD-gIVwm1vBB0CFQx_EAAYAiAAEgLu4vD_BwE
illegal.except.life <- c("AL","AR","ID","KY","LA","MO","MS","ND","OK","SD","TN","TX")

#https://usafacts.org/articles/thirteen-states-have-trigger-laws-banning-abortion-if-roe-v-wade-is-overruled/?utm_source=google&utm_medium=cpc&utm_campaign=ND-Healthcare&gclid=EAIaIQobChMIporqkMaD-gIVRilMCh3rQwE7EAAYASAAEgIA-vD_BwE
trigger.law <- c("AR","ID","KY","LA","MO","MS","ND","OK","SD","TN","TX","UT","WY")

# https://reproductiverights.org/maps/abortion-laws-by-state/
law.to.ban <- c("UT","WI","WV","WY","SC")

#https://www.poynter.org/fact-checking/2022/post-roe-v-wade-state-bans-no-exceptions-rape-incest/
no.rape.exception <- c("AL","KY","MO","TN","LA","OK","SD","TX","AZ","WI","MI","WV","AR","FL","OH")

#https://www.nytimes.com/interactive/2022/us/abortion-laws-roe-v-wade.html
nytimes.restriction <- c("AL","AZ","AR","ID","KY","LA","MO","OK","SD","TN","TX","WV","WI")

abortdat <- abortdat %>%
  mutate(TriggerState = ifelse(ts_state %in% trigger.law,"Illegal","Legal"),
         Active.Illegal = ifelse(ts_state %in% illegal.except.life,"Illegal","Legal"),
         BanPassed = ifelse(ts_state %in% trigger.law | ts_state %in% illegal.except.life | ts_state %in% law.to.ban,"Illegal","Legal"),
         NoRapeExcept = ifelse(ts_state %in% no.rape.exception,"Illegal","Legal"),
         NYTNoRapeExcept = ifelse(ts_state %in% nytimes.restriction,"Illegal","Legal"),
         OwnPID = ifelse(OwnPID == "", "Independent", OwnPID))

scale <- abortdat %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "Donor (2018)") %>%
  group_by(NoRapeExcept) %>%
  count(abortscale_f) %>%
  filter(!is.na(abortscale_f)) %>%
  mutate(Pct = n/sum(n))

#Figure F5
scale %>%
  ggplot(aes(x=abortscale_f, y = Pct, fill=NoRapeExcept)) +
  geom_col(position="dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By states with exception if pregnancy is the result of rape",
       fill="Rape Exception") + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))


#################################################################################################
# Figure 1B: By Sample Among Republicans, Unweighted
#################################################################################################

abortdat %>%
  filter(OwnPID == "Republican") %>%
  count(sample3)

abortdat %>%
  filter(OwnPID == "Republican" & sample3 =="Donor (2018)") %>%
  count(i_abortion6)

rawdat <- abortdat %>%
  group_by(sample3) %>%
  filter(OwnPID == "Republican") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 


rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

rawdat$OwnPID <- factor(rawdat$OwnPID, levels = c("Democrat", "Independent", "Republican"))

rawdat$sample <- factor(rawdat$sample3, levels = c("Donor (2018)", "Affluent", "General Population"))

#Figure 2b
rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=sample)) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "Among Republicans: Donors, Affluent, and General Population",
       fill="") + 
  scale_x_discrete(labels=element_blank()) + 
  scale_fill_manual(values = c("#009E73", "#0072B2","#D55E00"), labels = c("Donor", "Affluent", "General Population")) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() + 
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE)) + 
  theme(legend.position = "bottom")

sumdat1b <- rawdat %>% select(Variable_f, value, sample) %>% 
  group_by(sample, Variable_f) %>%
  summarize(mean = mean(value, na.rm = T))
sumdat1b


########################################################################################
# SI: Figure 1B: Weighted Means by Group
########################################################################################
#Get Gen Pop Weighted Means

weightedRepGenPop <- w.mean %>%
  filter(OwnPID=="Republican") %>%
  mutate(sample3 = "General Population")

weightedRepGenPop <- weightedRepGenPop %>%
  select(Variable, Mean, sample3)

######################################################################################
#Get the wealthy weighted data
dat <- read_dta("~/Desktop/POQ_replication.dta")
wgt <- read_csv(file="~/Desktop/WealthyWgts.csv")

dat <- left_join(dat,wgt)

dat <- dat %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc") %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_gender = ifelse(d_gender==1,"Male","Female"),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_CollegePlus = ifelse(d_education > 4,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,"Most Important","Not Most Important"),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         vreligious = ifelse(d_religveryimp ==1 & d_religattendweekly==1,1,NA),
         vreligious = ifelse(d_religveryimp ==0 & d_religattendweekly==0,0,vreligious)) %>%
  select(-c(i_abortion1,i_abortion2))

dat$sample3 <- factor(dat$sample3, 
                      levels=c('Donor (2018)', 'Affluent', 'General Population'))

abortdat <- dat %>%
  select(OwnPID,sample3,d_gender,RakingWgt,d_CollegePlus,vreligious,ts_state,imp_abortion,imp_abortionmost,starts_with("i_abort"))

abortdat <- abortdat %>%
  mutate(OwnPID = ifelse(OwnPID == "", "Independent", OwnPID))


# Make Affluent Weighted Means
rawdat <- abortdat %>%
  filter(OwnPID == "Republican" & sample3 == "Affluent") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

weightedRepAffluent <- rawdat %>%
  group_by(Variable) %>%
  summarize(Mean = weighted.mean(value, RakingWgt, na.rm=TRUE)) %>%
  mutate(sample3 = "Affluent")


######################################################################################
# Get Donor Weights
dat <- read_dta("~/Desktop/POQ_replication.dta")
wgt <- read_csv("~/Desktop/DonorWgts.csv")

dat <- left_join(dat,wgt)

dat <- dat %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc") %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_gender = ifelse(d_gender==1,"Male","Female"),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_CollegePlus = ifelse(d_education > 4,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,"Most Important","Not Most Important"),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         vreligious = ifelse(d_religveryimp ==1 & d_religattendweekly==1,1,NA),
         vreligious = ifelse(d_religveryimp ==0 & d_religattendweekly==0,0,vreligious)) %>%
  select(-c(i_abortion1,i_abortion2))

dat$sample3 <- factor(dat$sample3, 
                      levels=c('Donor (2018)', 'Affluent', 'General Population'))

abortdat <- dat %>%
  select(OwnPID,sample3,d_gender,RakingWgt,d_CollegePlus,vreligious,ts_state,imp_abortion,imp_abortionmost,starts_with("i_abort"))

abortdat <- abortdat %>%
  mutate(OwnPID = ifelse(OwnPID == "", "Independent", OwnPID))

# Make Donor Weighted Means
rawdat <- abortdat %>%
  filter(OwnPID == "Republican" & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

weightedRepDonor <- rawdat %>%
  group_by(Variable) %>%
  summarize(Mean = weighted.mean(value, RakingWgt, na.rm=TRUE)) %>%
  mutate(sample3 = "Donors (2018)")

#bind the three weighted samples together
w.dat <- bind_rows(weightedRepGenPop, weightedRepDonor, weightedRepAffluent)
w.dat <- as.data.frame(w.dat)

#make the plot (keep ordering the same as before)
mean <- tapply(rawdat$value, rawdat$Variable, mean, na.rm=TRUE)
poo <- sort(mean)
w.dat$Variable_f <- factor(w.dat$Variable, levels = names(poo))

w.dat$sample <- NULL
w.dat$sample <- factor(w.dat$sample3, levels = c("Donors (2018)", "Affluent", "General Population"))

#Figure C2
w.dat %>%
  ggplot(aes(x=Mean, y=Variable_f, fill=sample)) +
  geom_col(position="dodge") +
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "Among Weighted Republican Donors, Affluent, and General Population",
       fill="") + 
  scale_y_discrete(labels=wrap_format(30)) + 
  scale_fill_manual(values = c("#009E73", "#0072B2","#D55E00"), labels = c("Donor", "Affluent", "General Population")) +
  scale_x_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() + 
  geom_vline(xintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE)) + 
  theme(legend.position = "bottom")




#################################################################################################
# Figure3 Panel A: By Most Importance Among Republican Donors
#################################################################################################
dat <- read_dta("~/Desktop/POQ_replication.dta")

dat <- dat %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc") %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_gender = ifelse(d_gender==1,"Male","Female"),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_CollegePlus = ifelse(d_education > 4,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,"Most Important","Not Most Important"),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         vreligious = ifelse(d_religveryimp ==1 & d_religattendweekly==1,1,NA),
         vreligious = ifelse(d_religveryimp ==0 & d_religattendweekly==0,0,vreligious)) %>%
  select(-c(i_abortion1,i_abortion2))

dat$sample3 <- factor(dat$sample3, 
                      levels=c('Donor (2018)', 'Affluent', 'General Population'))

abortdat <- dat %>%
  select(OwnPID, sample3, imp_abortionmost, starts_with("i_abort"))

abortdat %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "Donor (2018)") %>%
  count(imp_abortionmost) %>%
  mutate(Pct = n/sum(n))

rawdat <- abortdat %>%
  group_by(imp_abortionmost) %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) %>%
  mutate(imp_abortionmost = ifelse(imp_abortionmost=="Most Important","Most",imp_abortionmost),
         imp_abortionmost = ifelse(imp_abortionmost=="Not Most Important","Not",imp_abortionmost))

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value, rawdat$Variable, mean, na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure 3A
Fig3A <- rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(imp_abortionmost))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By issue importance",
       fill="Importance") + 
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))
Fig3A

sumdat3a <- rawdat %>% select(Variable_f, value, imp_abortionmost) %>% 
  group_by(imp_abortionmost, Variable_f) %>%
  summarize(mean = mean(value, na.rm = T))
sumdat3a


################################################################################################
# SI - FIGURE 3A WITH WEIGHTED OPINION  
################################################################################################
dat <- read_dta("~/Desktop/POQ_replication.dta")
wgt <- read_csv(file="~/Desktop/DonorWgts.csv")
dat <- left_join(dat,wgt)

dat <- dat %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc") %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_gender = ifelse(d_gender==1,"Male","Female"),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_CollegePlus = ifelse(d_education > 4,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,"Most Important","Not Most Important"),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         vreligious = ifelse(d_religveryimp ==1 & d_religattendweekly==1,1,NA),
         vreligious = ifelse(d_religveryimp ==0 & d_religattendweekly==0,0,vreligious)) %>%
  select(-c(i_abortion1,i_abortion2))

dat$sample3 <- factor(dat$sample3, 
                      levels=c('Donor (2018)', 'Affluent', 'General Population'))

abortdat <- dat %>%
  select(OwnPID, sample3, RakingWgt, imp_abortionmost, starts_with("i_abort"))

rawdat <- abortdat %>%
  group_by(imp_abortionmost) %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) %>%
  mutate(imp_abortionmost = ifelse(imp_abortionmost=="Most Important","Most",imp_abortionmost),
         imp_abortionmost = ifelse(imp_abortionmost=="Not Most Important","Not",imp_abortionmost)) %>%
  group_by(imp_abortionmost)


rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

w.mean <- rawdat %>%
  group_by(Variable, imp_abortionmost) %>%
  summarize(Mean = weighted.mean(value, RakingWgt, na.rm=TRUE))

mean <- tapply(rawdat$value, rawdat$Variable, mean, na.rm=TRUE)
poo <- sort(mean)
w.mean$Variable_f <- factor(w.mean$Variable, levels = names(poo))

w.mean <- as.data.frame(w.mean)

#Figure C3
w.mean %>% 
  ggplot(aes(x=Mean, y=Variable_f, fill=factor(imp_abortionmost))) +
  geom_col(position="dodge") +
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By issue importance",
       fill="Importance") + 
  scale_y_discrete(labels=wrap_format(30)) +
  scale_x_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_vline(xintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))


################################################################################################
# SI Figure3 Panel A: By Most Importance Among Republican AFFLUENT 
#(instead of donor, as is in the paper)
################################################################################################

abortdat %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "Affluent") %>%
  count(imp_abortionmost) %>%
  mutate(Pct = n/sum(n))

rawdat <- abortdat %>%
  group_by(imp_abortionmost) %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "Affluent") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) %>%
  mutate(imp_abortionmost = ifelse(imp_abortionmost=="Most Important","Most",imp_abortionmost),
         imp_abortionmost = ifelse(imp_abortionmost=="Not Most Important","Not",imp_abortionmost))

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure D1
rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(imp_abortionmost))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By issue importance (among Affluent Republicans)",
       fill="Importance") + 
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))



#################################################################################################
# SI Figure3 Panel A: By Most Importance Among Republican Gen Pop 
#(as opposed to donors in the main paper)
#################################################################################################

abortdat %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "General Population") %>%
  count(imp_abortionmost) %>%
  mutate(Pct = n/sum(n))

rawdat <- abortdat %>%
  group_by(imp_abortionmost) %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortionmost) & sample3 == "General Population") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) %>%
  mutate(imp_abortionmost = ifelse(imp_abortionmost=="Most Important","Most",imp_abortionmost),
         imp_abortionmost = ifelse(imp_abortionmost=="Not Most Important","Not",imp_abortionmost))

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value, rawdat$Variable, mean, na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure D3
rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(imp_abortionmost))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By issue importance (among general population Republicans)",
       fill="Importance") + 
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))


################################################################################################
# Fig 3 Panel B: Republican Donor Support by State Exception Status
################################################################################################
dat <- read_dta("~/Desktop/POQ_replication.dta")

dat <- dat %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc") %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_gender = ifelse(d_gender==1,"Male","Female"),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_CollegePlus = ifelse(d_education > 4,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,"Most Important","Not Most Important"),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         vreligious = ifelse(d_religveryimp ==1 & d_religattendweekly==1,1,NA),
         vreligious = ifelse(d_religveryimp ==0 & d_religattendweekly==0,0,vreligious)) %>%
  select(-c(i_abortion1,i_abortion2))

dat$sample3 <- factor(dat$sample3, 
                      levels=c('Donor (2018)', 'Affluent', 'General Population'))

abortdat <- dat %>%
  select(ts_state, OwnPID, sample3, imp_abortionmost, starts_with("i_abort"))

# https://www.guttmacher.org/state-policy/explore/overview-abortion-laws?gclid=EAIaIQobChMIm8uy-cSD-gIVwm1vBB0CFQx_EAAYAiAAEgLu4vD_BwE
illegal.except.life <- c("AL","AR","ID","KY","LA","MO","MS","ND","OK","SD","TN","TX")

#https://usafacts.org/articles/thirteen-states-have-trigger-laws-banning-abortion-if-roe-v-wade-is-overruled/?utm_source=google&utm_medium=cpc&utm_campaign=ND-Healthcare&gclid=EAIaIQobChMIporqkMaD-gIVRilMCh3rQwE7EAAYASAAEgIA-vD_BwE
trigger.law <- c("AR","ID","KY","LA","MO","MS","ND","OK","SD","TN","TX","UT","WY")

# https://reproductiverights.org/maps/abortion-laws-by-state/
law.to.ban <- c("UT","WI","WV","WY","SC")

#https://www.poynter.org/fact-checking/2022/post-roe-v-wade-state-bans-no-exceptions-rape-incest/
no.rape.exception <- c("AL","KY","MO","TN","LA","OK","SD","TX","AZ","WI","MI","WV","AR","FL","OH")

#https://www.nytimes.com/interactive/2022/us/abortion-laws-roe-v-wade.html
nytimes.restriction <- c("AL","AZ","AR","ID","KY","LA","MO","OK","SD","TN","TX","WV","WI")

abortdat <- abortdat %>%
  mutate(TriggerState = ifelse(ts_state %in% trigger.law,"Illegal","Legal"),
         Active.Illegal = ifelse(ts_state %in% illegal.except.life,"Illegal","Legal"),
         BanPassed = ifelse(ts_state %in% trigger.law | ts_state %in% illegal.except.life | ts_state %in% law.to.ban,"Illegal","Legal"),
         NoRapeExcept = ifelse(ts_state %in% no.rape.exception,"Illegal","Legal"),
         NYTNoRapeExcept = ifelse(ts_state %in% nytimes.restriction,"Illegal","Legal"),
         OwnPID = ifelse(OwnPID == "", "Independent", OwnPID))

rawdat <- abortdat %>%
  group_by(NoRapeExcept) %>%
  filter(OwnPID == "Republican" & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

rawdat$NoRapeExcept2 <- ifelse(rawdat$NoRapeExcept == "Illegal", "Not Present (Illegal)" ,"Present (Legal)")

mean <- tapply(rawdat$value, rawdat$Variable, mean, na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure 3B
Fig3B <- rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(NoRapeExcept2))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By states with exception if pregnancy is the result of rape",
       fill="Rape Exception") + 
  scale_x_discrete(labels=element_blank()) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L), limits = c(0, 1)) + 
  theme_bw() +
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))
Fig3B

sumdat3b <- rawdat %>% select(Variable_f, value, NoRapeExcept2) %>% 
  group_by(NoRapeExcept2, Variable_f) %>%
  summarize(mean = mean(value, na.rm = T))
sumdat3b


##############################################################################################
# SI - FIGURE 3B WITH WEIGHTED OPINION Support by State Exception Status  
##############################################################################################

dat <- read_dta("~/Desktop/POQ_replication.dta")
wgt <- read_csv(file="~/Desktop/DonorWgts.csv")
dat <- left_join(dat,wgt)

dat <- dat %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc") %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_gender = ifelse(d_gender==1,"Male","Female"),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_CollegePlus = ifelse(d_education > 4,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,"Most Important","Not Most Important"),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         vreligious = ifelse(d_religveryimp ==1 & d_religattendweekly==1,1,NA),
         vreligious = ifelse(d_religveryimp ==0 & d_religattendweekly==0,0,vreligious)) %>%
  select(-c(i_abortion1,i_abortion2))

dat$sample3 <- factor(dat$sample3, 
                      levels=c('Donor (2018)', 'Affluent', 'General Population'))

abortdat <- dat %>%
  select(ts_state, OwnPID, sample3, RakingWgt, imp_abortionmost, starts_with("i_abort"))

abortdat <- abortdat %>%
  mutate(TriggerState = ifelse(ts_state %in% trigger.law,"Illegal","Legal"),
         Active.Illegal = ifelse(ts_state %in% illegal.except.life,"Illegal","Legal"),
         BanPassed = ifelse(ts_state %in% trigger.law | ts_state %in% illegal.except.life | ts_state %in% law.to.ban,"Illegal","Legal"),
         NoRapeExcept = ifelse(ts_state %in% no.rape.exception,"Illegal","Legal"),
         NYTNoRapeExcept = ifelse(ts_state %in% nytimes.restriction,"Illegal","Legal"),
         OwnPID = ifelse(OwnPID == "", "Independent", OwnPID))

rawdat <- abortdat %>%
  group_by(NoRapeExcept) %>%
  filter(OwnPID == "Republican" & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

rawdat$NoRapeExcept2 <- ifelse(rawdat$NoRapeExcept == "Illegal", "Not Present (Illegal)" ,"Present (Legal)")

w.mean <- rawdat %>%
  group_by(Variable, NoRapeExcept2) %>%
  summarize(Mean = weighted.mean(value, RakingWgt, na.rm=TRUE))

mean <- tapply(rawdat$value, rawdat$Variable, mean, na.rm=TRUE)
poo <- sort(mean)
w.mean$Variable_f <- factor(w.mean$Variable, levels = names(poo))

w.mean <- as.data.frame(w.mean)

#Figure C4
w.mean %>% 
  ggplot(aes(x=Mean, y=Variable_f, fill=factor(NoRapeExcept2))) +
  geom_col(position="dodge") +
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By states with exception if pregnancy is the result of rape",
       fill="Rape Exception") + 
  scale_y_discrete(labels=wrap_format(30)) +
  scale_x_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_vline(xintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))



################################################################################################
# SI Fig3 Panel B: Republican Affluent (instead of donor) Support by State Exception Status
################################################################################################

abortdat <- dat %>%
  select(ts_state, OwnPID, sample3, imp_abortionmost, starts_with("i_abort"))

abortdat <- abortdat %>%
  mutate(TriggerState = ifelse(ts_state %in% trigger.law,"Illegal","Legal"),
         Active.Illegal = ifelse(ts_state %in% illegal.except.life,"Illegal","Legal"),
         BanPassed = ifelse(ts_state %in% trigger.law | ts_state %in% illegal.except.life | ts_state %in% law.to.ban,"Illegal","Legal"),
         NoRapeExcept = ifelse(ts_state %in% no.rape.exception,"Illegal","Legal"),
         NYTNoRapeExcept = ifelse(ts_state %in% nytimes.restriction,"Illegal","Legal"),
         OwnPID = ifelse(OwnPID == "", "Independent", OwnPID))

abortdat %>%
  filter(OwnPID == "Republican" & !is.na(NoRapeExcept) & sample3 == "Affluent") %>%
  count(NoRapeExcept) %>%
  mutate(Pct = n/sum(n))

rawdat <- abortdat %>%
  group_by(NoRapeExcept) %>%
  filter(OwnPID == "Republican" & sample3 == "Affluent") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 


rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

rawdat$NoRapeExcept2 <- ifelse(rawdat$NoRapeExcept == "Illegal", "Not Present (Illegal)" ,"Present (Legal)")

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure D2
rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(NoRapeExcept2))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By states with exception if pregnancy is the result of rape (among affluent Republicans)",
       fill="Rape Exception") + 
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))



################################################################################################
# SI Fig 3 Panel B: Republican Gen Pop Support by State Exception Status
###############################################################################################

abortdat %>%
  filter(OwnPID == "Republican" & !is.na(NoRapeExcept) & sample3 == "General Population") %>%
  count(NoRapeExcept) %>%
  mutate(Pct = n/sum(n))

rawdat <- abortdat %>%
  group_by(NoRapeExcept) %>%
  filter(OwnPID == "Republican" & sample3 == "General Population") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

rawdat$NoRapeExcept2 <- ifelse(rawdat$NoRapeExcept == "Illegal", "Not Present (Illegal)" ,"Present (Legal)")

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure D4
rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(NoRapeExcept2))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By states with exception if pregnancy is the result of rape (among general population Republicans)",
       fill="Rape Exception") + 
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() +   
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))

################################################################################################
# SI Fig 3 Panel B: Republican Donor Support by NYT
################################################################################################

abortdat %>%
  filter(OwnPID == "Republican" & !is.na(NYTNoRapeExcept) & sample3 == "Affluent") %>%
  count(NYTNoRapeExcept) %>%
  mutate(Pct = n/sum(n))

rawdat <- abortdat %>%
  group_by(NYTNoRapeExcept) %>%
  filter(OwnPID == "Republican" & sample3 == "Affluent") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure F6
rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(NYTNoRapeExcept))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By states with exception if pregnancy is the result of rape",
       fill="Rape Exception") + 
  scale_x_discrete(labels=wrap_format(30)) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L), limits = c(0, 1)) + 
  theme_bw() +
  theme(legend.position="bottom") +
  geom_hline(yintercept=.5,lty=2) +
  guides(fill = guide_legend(reverse = TRUE))


#################################################################################################
# Supplemental: By 4 year degree or more Among Republican Donors
#################################################################################################

abortdat <- dat %>%
  select(d_CollegePlus, ts_state, vreligious, d_gender, OwnPID, sample3, RakingWgt, imp_abortion, imp_abortionmost, starts_with("i_abort"))

rawdat <- abortdat %>%
  filter(sample3 == "Donor (2018)") %>%
  group_by(d_CollegePlus) %>%
  filter(OwnPID == "Republican") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat <- rawdat %>%
  mutate(d_CollegePlus = ifelse(d_CollegePlus==1,"Yes","No"))

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure E1
rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(d_CollegePlus))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.75, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By educational attainment (among Republican donors)",
       fill="4 Year Degree +") + 
  scale_x_discrete(labels=wrap_format(30)) +
  geom_hline(yintercept=.5,lty=2) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) +
  theme_bw() + 
  guides(fill = guide_legend(reverse = TRUE))


#################################################################################################
# Supplemental: By Gender Among Republican Donors
#################################################################################################

rawdat <- abortdat %>%
  filter(sample3 == "Donor (2018)") %>%
  group_by(d_gender) %>%
  filter(OwnPID == "Republican") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure E2
rawdat %>%
  filter(d_gender=="Male" | d_gender == "Female") %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(d_gender))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.75, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By gender (among Republican donors)",
       fill="") + 
  scale_x_discrete(labels=wrap_format(30)) +
  geom_hline(yintercept=.5,lty=2) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) + 
  theme_bw() +
  guides(fill = guide_legend(reverse = TRUE))


################################################################################################
# Supplemental: By Very Religious Among Republican Donors
################################################################################################

rawdat <- abortdat %>%
  filter(sample3 == "Donor (2018)") %>%
  group_by(vreligious) %>%
  filter(OwnPID == "Republican" & !is.na(imp_abortion)) %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat <- rawdat %>%
  mutate(vreligious = ifelse(vreligious==1,"Yes","No"))

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure E3
rawdat %>%
  filter(vreligious=="Yes" | vreligious=="No") %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(vreligious))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.75, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By religiousity (among Republican donors)",
       fill="Very Religious") + 
  scale_x_discrete(labels=wrap_format(30)) +
  geom_hline(yintercept=.5,lty=2) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) + 
  theme_bw() +
  guides(fill = guide_legend(reverse = TRUE))


################################################################################################
# Additional Descriptive Plots: Republican Donors in States with Rape Bans
################################################################################################


################################################################################################
# Rep Donors in Ban States by Most Important
################################################################################################

abortdat <- abortdat %>%
  mutate(TriggerState = ifelse(ts_state %in% trigger.law,"Illegal","Legal"),
         Active.Illegal = ifelse(ts_state %in% illegal.except.life,"Illegal","Legal"),
         BanPassed = ifelse(ts_state %in% trigger.law | ts_state %in% illegal.except.life | ts_state %in% law.to.ban,"Illegal","Legal"),
         NoRapeExcept = ifelse(ts_state %in% no.rape.exception,"Illegal","Legal"),
         NYTNoRapeExcept = ifelse(ts_state %in% nytimes.restriction,"Illegal","Legal"),
         OwnPID = ifelse(OwnPID == "", "Independent", OwnPID))

rawdat <- abortdat %>%
  filter(NoRapeExcept == "Illegal" & OwnPID == "Republican" & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat <- abortdat %>%
  filter(NoRapeExcept == "Illegal" & OwnPID == "Republican" & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) %>%
  group_by(imp_abortionmost)

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

rawdat$NoRapeExcept2 <- ifelse(rawdat$NoRapeExcept == "Illegal", "Not Present (Illegal)" ,"Present (Legal)")

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure D5
rawdat %>%
  filter(!is.na(imp_abortionmost)) %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(imp_abortionmost))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.75, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By issue importance\n(in states without exception if pregnancy is the result of rape, among Republican donors)",
       fill="") + 
  scale_x_discrete(labels=wrap_format(30)) +
  geom_hline(yintercept=.5,lty=2) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) + 
  theme_bw() +
  guides(fill = guide_legend(reverse = TRUE))



################################################################################################
# Supp: Rep Donors in Ban States by Very religious
################################################################################################

rawdat <- abortdat %>%
  filter(NoRapeExcept == "Illegal" & OwnPID == "Republican" & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) %>%
  group_by(vreligious)

rawdat <- rawdat %>%
  mutate(vreligious = ifelse(vreligious==1,"Yes","No"))

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure E4
rawdat %>%
  filter(!is.na(vreligious)) %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(vreligious))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#D55E00", "#999999")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.75, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By religiousity\n(in states without exception if pregnancy is the result of rape, among Republican donors)",
       fill="Very Religious") + 
  scale_x_discrete(labels=wrap_format(30)) +
  geom_hline(yintercept=.5,lty=2) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) + 
  theme_bw() +
  guides(fill = guide_legend(reverse = TRUE))


################################################################################################
# Supp: Rep Donors in Ban States by Very Religious & Most Important
################################################################################################

abortdat %>%
  filter(NoRapeExcept == "Illegal" & OwnPID == "Republican" & sample3 == "Donor (2018)") %>%
  count(imp_abortionmost,vreligious)

abortdat <- abortdat %>%
  mutate(religimp = ifelse(vreligious==1 & imp_abortionmost=="Not Most Important","Religious, Not most imp issue",NA),
         religimp = ifelse(vreligious==1 & imp_abortionmost=="Most Important","Religious, Most imp issue",religimp),
         religimp = ifelse(vreligious==0 & imp_abortionmost=="Not Most Important","Not Religious, Not most imp issue",religimp),
         religimp = ifelse(vreligious==0 & imp_abortionmost=="Most Important","Not Religious, Most imp issue",religimp))

abortdat$religimp <- factor(abortdat$religimp,
                            levels=c("Religious, Most imp issue","Religious, Not most imp issue","Not Religious, Most imp issue","Not Religious, Not most imp issue"))

rawdat <- abortdat %>%
  filter(NoRapeExcept == "Illegal" & OwnPID == "Republican" & sample3 == "Donor (2018)") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) %>%
  group_by(religimp)

rawdat<- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable)  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure E5
rawdat %>%
  filter((vreligious==1 | vreligious==0) & (imp_abortionmost=="Most Important" | imp_abortionmost=="Not Most Important")) %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(religimp))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#440154FF","#31688EFF","#35B779FF","#FDE725FF")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.9, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By religiousity and issue importance\n(in states without exception if pregnancy is the result of rape, among Republican donors)",
       fill="") + 
  scale_x_discrete(labels=wrap_format(30)) +
  geom_hline(yintercept=.5,lty=2) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) + 
  theme_bw() + 
  guides(fill = guide_legend(reverse = TRUE))


######################################################################################
# Get Donor Weights
# Repliacate Figure 4 Panel A: With Weights
#######################################################################################

dat <- read_dta("~/Desktop/POQ_replication.dta")
wgt <- read_csv(file="~/Desktop/DonorWgts.csv")
dat <- left_join(dat,wgt)

dat <- dat %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc") %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_gender = ifelse(d_gender==1,"Male","Female"),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_CollegePlus = ifelse(d_education > 4,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,"Most Important","Not Most Important"),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         vreligious = ifelse(d_religveryimp ==1 & d_religattendweekly==1,1,NA),
         vreligious = ifelse(d_religveryimp ==0 & d_religattendweekly==0,0,vreligious)) %>%
  select(-c(i_abortion1,i_abortion2))

dat$sample3 <- factor(dat$sample3, 
                      levels=c('Donor (2018)', 'Affluent', 'General Population'))

abortdat <- dat %>%
  select(OwnPID,sample3,d_gender,RakingWgt,d_CollegePlus,vreligious,ts_state,imp_abortion,imp_abortionmost,starts_with("i_abort"), starts_with("sa_"))


# https://www.guttmacher.org/state-policy/explore/overview-abortion-laws?gclid=EAIaIQobChMIm8uy-cSD-gIVwm1vBB0CFQx_EAAYAiAAEgLu4vD_BwE
illegal.except.life <- c("AL","AR","ID","KY","LA","MO","MS","ND","OK","SD","TN","TX")

#https://usafacts.org/articles/thirteen-states-have-trigger-laws-banning-abortion-if-roe-v-wade-is-overruled/?utm_source=google&utm_medium=cpc&utm_campaign=ND-Healthcare&gclid=EAIaIQobChMIporqkMaD-gIVRilMCh3rQwE7EAAYASAAEgIA-vD_BwE
trigger.law <- c("AR","ID","KY","LA","MO","MS","ND","OK","SD","TN","TX","UT","WY")

# https://reproductiverights.org/maps/abortion-laws-by-state/
law.to.ban <- c("UT","WI","WV","WY","SC")

#https://www.poynter.org/fact-checking/2022/post-roe-v-wade-state-bans-no-exceptions-rape-incest/
no.rape.exception <- c("AL","KY","MO","TN","LA","OK","SD","TX","AZ","WI","MI","WV","AR","FL","OH")

abortdat <- abortdat %>%
  mutate(TriggerState = ifelse(ts_state %in% trigger.law,"Illegal","Legal"),
         Active.Illegal = ifelse(ts_state %in% illegal.except.life,"Illegal","Legal"),
         BanPassed = ifelse(ts_state %in% trigger.law | ts_state %in% illegal.except.life | ts_state %in% law.to.ban,"Illegal","Legal"),
         NoRapeExcept = ifelse(ts_state %in% no.rape.exception,"Illegal","Legal"),
         OwnPID = ifelse(OwnPID == "", "Independent", OwnPID))

rawdat <- abortdat %>%
  mutate(activism_scale = rowSums(across(starts_with("sa_")), na.rm = T),
         activist = case_when(activism_scale >=5 ~ "High (>5)",
                              activism_scale < 5 & activism_scale > 1 ~ "Mid (2-4)",
                              activism_scale <=1 ~ "Low (0-1)")) %>%
  group_by(activist) %>%
  filter(OwnPID == "Republican") %>%
  pivot_longer(names_to = "Variable", cols = starts_with("i_abort")) 

rawdat$activist <- factor(rawdat$activist, levels = c("High (>5)", "Mid (2-4)", "Low (0-1)"))

rawdat <- rawdat %>%
  mutate(Variable = ifelse(Variable=="i_abortion3","If her health is seriously endangered by the pregnancy",Variable),
         Variable = ifelse(Variable=="i_abortion4","If she is carrying a fetus with a serious intellectual disability, such as Down Syndrome",Variable),
         Variable = ifelse(Variable=="i_abortion5","If she is carrying a fetus with a serious birth defect likely to require serious medical care and limit quality of life",Variable),
         Variable = ifelse(Variable=="i_abortion6","If she becomes pregnant as the result of rape",Variable),
         Variable = ifelse(Variable=="i_abortion7","In the first trimester if she is married and her husband objects to the abortion",Variable),        
         Variable = ifelse(Variable=="i_abortion8","In the first trimester if she is under 18 and her parents do not want her to have an abortion",Variable),  
         Variable = ifelse(Variable=="i_abortion9","In the first trimester of pregnancy under any circumstance, as a matter of choice",Variable),
         Variable = ifelse(Variable=="i_abortion10","In the second or third trimester of pregnancy under any circumstance, as a matter of choice",Variable),  
  )

mean <- tapply(rawdat$value,rawdat$Variable,mean,na.rm=TRUE)
poo <- sort(mean)
rawdat$Variable_f <- factor(rawdat$Variable, levels = names(poo))

#Figure E6
rawdat %>%
  ggplot(aes(x=Variable_f, y=value, fill=factor(activist))) +
  stat_summary(fun.data=mean_sdl, geom="bar",position = "dodge") +
  coord_flip() + 
  scale_fill_manual(values = c("#440154FF","#31688EFF","#35B779FF","#FDE725FF")) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=.75, position = "dodge") + 
  labs(x = "", y = "", title = "Support for abortion if...",
       subtitle = "By political activity level (among Republicans)",
       fill="") + 
  scale_x_discrete(labels=wrap_format(30)) +
  geom_hline(yintercept=.5,lty=2) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1L)) + 
  theme_bw() +
  guides(fill = guide_legend(reverse = TRUE))


#######################################################################################
# Regression stuff
#######################################################################################

survey <- read_dta("~/Desktop/POQ_replication.dta")

survey <- survey %>%
  filter(completedsurvey==1) %>%
  filter(sample3=="TS_Donor" | sample3=="TS_GenPop" | sample3=="TS_HighInc")

survey$d_age = 2019 - as.numeric(survey$d_yearborn)

survey <- survey %>%
  mutate(sample3 = ifelse(sample3=="TS_Donor","Donor (2018)",sample3),
         sample3 = ifelse(sample3=="TS_GenPop","General Population",sample3),
         sample3 = ifelse(sample3=="TS_HighInc","Affluent",sample3),
         d_HSorLess = ifelse(d_education < 3,1,0),
         d_SomeCollege = ifelse(d_education %in% c(3, 4),1,0),
         d_College4 = ifelse(d_education==5,1,0),
         d_PostGrad = ifelse(d_education==6,1,0),
         d_religveryimp = ifelse(d_religion==4,1,0),
         d_religattendweekly = ifelse(d_religionattend > 4,1,0),
         imp_abortionmost = ifelse(imp_abortion==4,1,0),
         d_democrat = ifelse(OwnPID=="Democrat",1,0),
         d_republican = ifelse(OwnPID=="Republican",1,0),
         d_under40 = ifelse(d_age < 40,1,0),
         d_over65 = ifelse(d_age > 65,1,0),
         d_income50less = ifelse(survey$d_income ==1, 1, 0),
         d_income50_100 = ifelse(survey$d_income ==2, 1, 0),
         d_income100more = ifelse(survey$d_income > 2 & survey$d_income < 11, 1, 0))

survey <- survey  %>%
  mutate(i_abortion1 = ifelse(i_abortion2 == 1,1,i_abortion1),
         i_abortion1 = ifelse(i_abortion2 == 2,2,i_abortion1),
         i_abortion1 = ifelse(i_abortion2 == 3,3,i_abortion1),
         i_abortion1 = ifelse(i_abortion2 == 4,4,i_abortion1),
         i_abortion1 = ifelse(i_abortion2 == 5,NA,i_abortion1)) %>%
  rename(abortscale = i_abortion1) %>%
  dplyr::select(-c(i_abortion2))

survey$sample3 <- factor(survey$sample3, levels=c('General Population', 'Donor (2018)', 'Affluent'))
survey$d_vote18_yn <- ifelse(survey$d_vote18 == 1, 1, 0)

survey <- survey %>%
  mutate(AbortNever = ifelse(abortscale==1,1,0))

# https://www.guttmacher.org/state-policy/explore/overview-abortion-laws?gclid=EAIaIQobChMIm8uy-cSD-gIVwm1vBB0CFQx_EAAYAiAAEgLu4vD_BwE
illegal.except.life <- c("AL","AR","ID","KY","LA","MO","MS","ND","OK","SD","TN","TX")

#https://usafacts.org/articles/thirteen-states-have-trigger-laws-banning-abortion-if-roe-v-wade-is-overruled/?utm_source=google&utm_medium=cpc&utm_campaign=ND-Healthcare&gclid=EAIaIQobChMIporqkMaD-gIVRilMCh3rQwE7EAAYASAAEgIA-vD_BwE
trigger.law <- c("AR","ID","KY","LA","MO","MS","ND","OK","SD","TN","TX","UT","WY")

# https://reproductiverights.org/maps/abortion-laws-by-state/
law.to.ban <- c("UT","WI","WV","WY","SC")

#https://www.poynter.org/fact-checking/2022/post-roe-v-wade-state-bans-no-exceptions-rape-incest/
no.rape.exception <- c("AL","KY","MO","TN","LA","OK","SD","TX","AZ","WI","MI","WV","AR","FL","OH")

survey <- survey %>%
  mutate(TriggerState = ifelse(ts_state %in% trigger.law,1,0),
         Active.Illegal = ifelse(ts_state %in% illegal.except.life,1,0),
         BanPassed = ifelse(ts_state %in% trigger.law | ts_state %in% illegal.except.life | ts_state %in% law.to.ban,1,0),
         NoRapeException = ifelse(ts_state %in% no.rape.exception,1,0))

# Plot 1 regression specification
# Republicans
table(survey$i_abortion6[survey$sample3 == "General Population" & survey$OwnPID == "Republican"])

#rape exception model (OLS)
r <- lm(i_abortion6 ~ imp_abortionmost + d_religveryimp + d_religattendweekly + d_parent + d_under40 + d_over65 + d_gender +  d_HSorLess + d_SomeCollege + d_College4 + d_homeowner + r_race_white + NoRapeException + d_income50less + d_income50_100 + as.factor(sample3), data=filter(survey, d_republican==1))
nobs(r)
summary(r)

#first trimester as a matter of choice model (OLS)
r2 <- lm(i_abortion9 ~ imp_abortionmost + d_religveryimp + d_religattendweekly + d_parent + d_under40 + d_over65 + d_gender +  d_HSorLess + d_SomeCollege + d_College4 + d_homeowner + r_race_white + NoRapeException + d_income50less + d_income50_100 + as.factor(sample3), data=filter(survey, d_republican==1))
nobs(r2)
summary(r2)

#rape exception model (Probit)
r.probit <- glm(i_abortion6 ~ imp_abortionmost + d_religveryimp + d_religattendweekly + d_parent + d_under40 + d_over65 + d_gender +  d_HSorLess + d_SomeCollege + d_College4 + d_homeowner + r_race_white + NoRapeException + d_income50less + d_income50_100 + as.factor(sample3), family = binomial(link="probit"), data=filter(survey,d_republican==1))
summary(r.probit)

#first trimester as a matter of choice model (Probit)
r2.probit <- glm(i_abortion9 ~ imp_abortionmost + d_religveryimp + d_religattendweekly + d_parent + d_under40 + d_over65 + d_gender +  d_HSorLess + d_SomeCollege + d_College4 + d_homeowner + r_race_white + NoRapeException + d_income50less + d_income50_100 + as.factor(sample3), data=filter(survey,d_republican==1), family = binomial(link="probit"))


#rape exception model (OLS, with state fixed effects)
r.fe <- lm(i_abortion6 ~ imp_abortionmost + d_religveryimp + d_religattendweekly + d_parent + d_under40 + d_over65 + d_gender +  d_HSorLess + d_SomeCollege + d_College4 + d_homeowner + r_race_white + NoRapeException + d_income50less + d_income50_100 + as.factor(sample3) + as.factor(ts_state), data=filter(survey, d_republican==1))
nobs(r)
summary(r.fe)

#first trimester as a matter of choice model (OLS, with state fixed effects)
r2.fe <- lm(i_abortion9 ~ imp_abortionmost + d_religveryimp + d_religattendweekly + d_parent + d_under40 + d_over65 + d_gender +  d_HSorLess + d_SomeCollege + d_College4 + d_homeowner + r_race_white + NoRapeException + d_income50less + d_income50_100 + as.factor(sample3) + as.factor(ts_state), data=filter(survey, d_republican==1))
nobs(r2.fe)
summary(r2.fe)


library(stargazer)
stargazer(r,r2,r.probit,r2.probit,r.fe,r2.fe,
          type="latex",
          report = "vcsp",
          out="SITable_Appendix6_Regression.results.tex",
          dep.var.caption = "",
          dep.var.labels  = c("If Rape","1st Trimester","If Rape","1st Trimester", "If Rape", "1st Trimester"),
          omit.stat = c("ser", "f","adj.rsq"),
          omit = "ts_state",
          covariate.labels = c("Abortion most important issue",
                               "Religion very important",
                               "Attend Church Weekly",
                               "Parent",
                               "Under 40",
                               "Age: Over 65",
                               "Male",
                               "Educ: HS or Less",
                               "Educ: Some College",
                               "Educ: Postgraduate",
                               "Homeowner",
                               "Race: White",
                               "State has no rape exception",
                               "Income: <$50k",
                               "Income: $50-100k",
                               "Sample: Donor",
                               "Sample: Affluent",
                               "Intercept"))


#FIGURE 4 - regression results
library(broom)
results <- tidy(r)

fit_95 <- confint(r, level = 0.95) %>% 
  data.frame() %>%
  rename("conf.low_95" = "X2.5..",
         "conf.high_95" = "X97.5..")


fit_90 <- confint(r, level = 0.90) %>% 
  data.frame() %>%
  rename("conf.low_90" = "X5..",
         "conf.high_90" = "X95..")

results <- bind_cols(results, 
                     fit_95, 
                     fit_90) %>%
  rename(Variable = term,
         Coefficient = estimate,
         SE = std.error) %>%
  filter(Variable != "(Intercept)")

results <- results %>% dplyr::select(-SE, 
                              -statistic,
                              -p.value) %>%
  mutate(Model="In cases of rape")

results2 <- tidy(r2)

fit_95 <- confint(r2, level = 0.95) %>% 
  data.frame() %>%
  rename("conf.low_95" = "X2.5..",
         "conf.high_95" = "X97.5..")


fit_90 <- confint(r2, level = 0.90) %>% 
  data.frame() %>%
  rename("conf.low_90" = "X5..",
         "conf.high_90" = "X95..")

results2 <- bind_cols(results2, 
                     fit_95, 
                     fit_90) %>%
  rename(Variable = term,
         Coefficient = estimate,
         SE = std.error) %>%
  filter(Variable != "(Intercept)")

results2 <- results2 %>% dplyr::select(-SE, 
                              -statistic,
                              -p.value) %>%
            mutate(Model="in first trimester")

results <- bind_rows(results, results2)

results <- results %>%
  mutate(Variable = ifelse(Variable == "d_parent","Parent",Variable),
         Variable = ifelse(Variable == "imp_abortionmost","Abortion most important issue",Variable),
         Variable = ifelse(Variable == "d_PostGrad","Educ: Postgraduate",Variable),
         Variable = ifelse(Variable == "as.factor(sample3)Affluent","Survey: Affluent",Variable),
         Variable = ifelse(Variable == "as.factor(sample3)Donor (2018)","Survey: Donor",Variable),
         Variable = ifelse(Variable == "r_race_white","Race: White",Variable),  
         Variable = ifelse(Variable == "d_HSorLess","Educ: HS or less",Variable), 
         Variable = ifelse(Variable == "d_SomeCollege","Educ: Some college",Variable),
         Variable = ifelse(Variable == "d_College4","Educ: 4-Year degree",Variable),
         Variable = ifelse(Variable == "d_gender","Male",Variable), 
         Variable = ifelse(Variable == "d_under40","Age: Under 40",Variable),  
         Variable = ifelse(Variable == "d_over65","Age: 65 or over",Variable), 
         Variable = ifelse(Variable == "d_homeowner","Homeowner",Variable), 
         Variable = ifelse(Variable == "d_religattendweekly","Religion: Weekly attendance",Variable), 
         Variable = ifelse(Variable == "d_religveryimp","Religion: Very important",Variable),
         Variable = ifelse(Variable == "NoRapeException","State Policy: no rape exception",Variable),
        Variable = ifelse(Variable == "d_income50less","Income: <$50k",Variable),
        Variable = ifelse(Variable == "d_income50_100","Income: $50-100k",Variable))

results$Variable <- factor(results$Variable, levels = rev(c("Abortion most important issue", "Religion: Very important", "Religion: Weekly attendance", "Educ: HS or less", "Educ: Some college", "Educ: 4-Year degree", "Income: <$50k", "Income: $50-100k", "Age: Under 40", "Age: 65 or over", "Race: White", "Parent", "Male", "Homeowner", "Survey: Donor", "Survey: Affluent", "State Policy: no rape exception")))

#Figure 4
ggplot(data = results) +
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) +
  geom_vline(xintercept = 3.5, colour = gray(1/2), lty = 1) +
  geom_vline(xintercept = 7.5, colour = gray(1/2), lty = 1) +
  geom_vline(xintercept = 9.5, colour = gray(1/2), lty = 1) +
  geom_vline(xintercept = 11.5, colour = gray(1/2), lty = 1) +
  geom_vline(xintercept = 14.5, colour = gray(1/2), lty = 1) +
  geom_point(aes(x = Variable, y = Coefficient, color=Model), position = position_dodge(width=.5)) + 
  geom_linerange(aes(x = Variable, ymin = conf.low_90, ymax = conf.high_90,color=Model), position = position_dodge(width=.5), lwd = 1) +
  geom_linerange(aes(x = Variable, ymin = conf.low_95, ymax = conf.high_95, color=Model), position = position_dodge(width=.5), lwd=.5) +
  scale_x_discrete(labels=wrap_format(20)) +
  scale_y_continuous(breaks = seq(-.5,.5, by = .1)) +
  scale_color_manual(values = c("#D55E00", "#999999")) +
  labs(x="", title = "Support for Legal Abortion among Republicans", subtitle= "Regression Intercept: In cases of rape = 0.88 \nRegression Intercept: In first trimester = 0.50", color = "Support for legal abortion:") +
  coord_flip() + 
  #theme_minimal() +
  theme(legend.position="bottom", 
        panel.background = element_blank(), 
        panel.border = element_rect(colour = "black", fill=NA, linewidth=.5),
        legend.key=element_blank()) + 
  geom_vline(xintercept = 1:17, colour = "grey90", lty = 2)



